package cc.hwtech.repaid;

import cn.org.rapid_framework.generator.Generator;
import cn.org.rapid_framework.generator.GeneratorFacade;
import cn.org.rapid_framework.generator.GeneratorProperties;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;

public class Gen {
    public static void generator(String configurationFile)
            throws Exception {
        String jarPath = Gen.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String templatePath = jarPath + "!/template";

        System.out.println(templatePath + " =====templatePath======");
        GeneratorProperties.load(new String[]{configurationFile});

        GeneratorFacade g = new GeneratorFacade();
        g.getGenerator().addTemplateRootDir(templatePath);


        g.deleteOutRootDir();


        String tables = GeneratorProperties.getProperty("tableNames");
        tables = tables.replaceAll(" ", "");
        String[] tableArr = tables.split(",");
        g.generateByTable(tableArr);

        //生成uri数据
        updateUriFile(tableArr);
    }

    private static void updateUriFile(String[] tableArr) {
        String uriFilePath = GeneratorProperties.getProperty("uriFilePath");
        File uriFile = new File(uriFilePath);
        BufferedReader bf = null;
        BufferedWriter bw = null;
        StringBuilder uriFileContentStrTemp = null;//临时保存Uri文件已有数据
        String uriFileContentStr = null;
        if ((uriFilePath == null) || ("".equals(uriFilePath))) {
            return;
        }
        try {
            bf = new BufferedReader(new InputStreamReader(new FileInputStream(uriFile), "UTF-8"));
            uriFileContentStrTemp = new StringBuilder();

            String lineStr = null;
            while ((lineStr = bf.readLine()) != null) {
                if (lineStr.indexOf("add repaid uri before here") > -1) {
                    uriFileContentStrTemp.append(getStringForAddUri(tableArr));
                }
                uriFileContentStrTemp.append(lineStr);
                uriFileContentStrTemp.append("\n");
            }
            //将临时保存Uri文件已有数据重新写入到文件中
            if (uriFileContentStrTemp.length() > 1) {
                uriFileContentStr = uriFileContentStrTemp.toString();
                bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(uriFile), "UTF-8"));
                bw.write(uriFileContentStr);
                bw.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bf != null) {
                try {
                    bf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                bf = null;
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                bw = null;
            }
        }
    }

    /**
     * 根据表明生成uri路径
     *
     * @param tableArr
     * @return
     */
    private static String getStringForAddUri(String[] tableArr) {
        String uriStr = "";
        for (String t : tableArr) {
            String tableUpperCase = t.toUpperCase();
            String tableLowerCase = t.toLowerCase();

            String oneUriStr = "\t/**\n\t * tableUpperCase\n\t */\n\tpublic static final String tableUpperCase_PAGE_SHOW = \"/tableLowerCase/page_show\";//页面\n\tpublic static final String tableUpperCase_ADD_SHOW = \"/tableLowerCase/add_show\";//添加-页面\n\tpublic static final String tableUpperCase_ADD_SUBMIT = \"/tableLowerCase/add_submit\";//添加-提交\n\tpublic static final String tableUpperCase_UPDATE_SHOW = \"/tableLowerCase/update_show\";//编辑-页面\n\tpublic static final String tableUpperCase_UPDATE_SUBMIT = \"/tableLowerCase/update_submit\";//编辑-提交\n\tpublic static final String tableUpperCase_DETAIL_SHOW = \"/tableLowerCase/detail_show\";//详情-页面\n\tpublic static final String tableUpperCase_DEL_SUBMIT = \"/tableLowerCase/del_submit\";//删除-提交\n\tpublic static final String tableUpperCase_DELETE_SUBMIT = \"/tableLowerCase/delete_submit\";//硬删除-提交\n\n";


            oneUriStr = oneUriStr.replaceAll("tableUpperCase", tableUpperCase);
            oneUriStr = oneUriStr.replaceAll("tableLowerCase", tableLowerCase);
            uriStr = uriStr + oneUriStr;
        }
        return uriStr;
    }

    public static void main(String[] args)
            throws Exception {
        String configurationFile = "classpath:repaid-generator.xml";
        generator(configurationFile);
    }
}
